//
//  HDWaitingViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/13.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDWaitingViewController.h"
#import "HDMenuView.h"
#import "HDWaitingTableViewController.h"

@interface HDWaitingViewController ()
@property(nonatomic,strong)HDMenuView *menu;
@property(nonatomic,strong)HDWaitingTableViewController *waitingList;
@property(nonatomic,strong)NSMutableArray *statusArray;    //记录子列表请求状态
@property(nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic)  DOImageView* tipView;
@end

@implementation HDWaitingViewController

- (void)initUI
{
    [self.view addSubview:self.menu];
    [self.view addSubview:self.waitingList.view];
    [self.view addSubview:self.tipView];
    
    self.statusArray = [[NSMutableArray alloc] initWithObjects:@NO, @NO, @NO, nil];
    NSMutableArray *todayArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *tomorrowArray = [[NSMutableArray alloc] initWithCapacity:0];
    NSMutableArray *dayAfterTomorrowArray = [[NSMutableArray alloc] initWithCapacity:0];
    self.dataArray = [[NSMutableArray alloc] initWithObjects:todayArray, tomorrowArray, dayAfterTomorrowArray, nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshOrderList:) name:HDRefreshOrderListNotification object:nil];
}


-(DOImageView*)tipView
{
    if (_tipView==nil)
    {
        float x=(APPFRAME.size.width-216)/2.0;
        float y=(SCREENFRAME.size.height-236)/2.0;
        CGRect rect=CGRectMake(x,y, 216, 118);
        _tipView=[[DOImageView alloc] initWithFrame:rect];
        _tipView.imgURLStr=@"bg1";
    }
    return _tipView;
}



- (void)refreshOrderList:(NSNotification *)note
{
    self.statusArray = [[NSMutableArray alloc] initWithObjects:@NO, @NO, @NO, nil];
    self.menu.selectedIndex = 0;
}

- (void)setOrderType:(HDOrderType)value
{
    _orderType = value;
    _waitingList.orderType = self.orderType;
}

-(void)updateViewController:(NSNotification *)note
{
    NSDictionary* dic=note.userInfo;
    if ([dic objectForKey:Receiver]==self.VctrID)
    {
        if (dic[@"OrderList"]!=nil)
        {
            self.statusArray[self.menu.selectedIndex] = @YES;
            self.waitingList.totalPagesNum = [[dic objectForKey:@"totalPage"] intValue];
            NSMutableArray *array = self.dataArray[self.menu.selectedIndex];
            if (self.waitingList.pageNum==1) {
                [array removeAllObjects];
            }
            NSMutableArray *tmpArray = self.dataArray[self.menu.selectedIndex];
            [tmpArray addObjectsFromArray:[dic objectForKey:@"dataArray"]];
            self.waitingList.dataArray = tmpArray;
            self.tipView.hidden=self.waitingList.dataArray.count;
        }
    }
}

- (void)selectMenuIndex:(int)index
{
    self.waitingList.dataArray = nil;
    self.waitingList.pageNum = 1;
    [self requestWaitingList:NO];
    self.tipView.hidden=self.waitingList.dataArray.count;
}

- (void)pullTableView:(UITableView *)tableView isNextPage:(BOOL)isNextPage
{
    self.waitingList.pageNum++;
    [self requestWaitingList:YES];
}

- (void)pullRefreshRequest
{
    self.waitingList.pageNum = 1;
    [self requestWaitingList:YES];
}

-(void)requestWaitingList:(BOOL)isPull
{
    NSString *pageNo = [NSString stringWithFormat:@"%d",self.waitingList.pageNum];
    UIView* hudView=isPull?nil:self.waitingList.view;
    //待上课请求
    NSDictionary* params=@{
                           @"coach_id":netEngine.coachInfo.m_id,
                           @"order_type":[NSNumber numberWithInt:self.orderType],//:1：教练 2：陪练://需要根据是学车界面还是练车界面选择
                           @"state":[NSNumber numberWithInt:4],
                           @"time_type":[NSNumber numberWithInt:self.menu.selectedIndex],
                           @"page":pageNo,
                           @"row":@"20"
                           };
    [netEngine RequestOrderListWithTarget:self.VctrID hudView:hudView Data:params];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _menu.selectedIndex = 0;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - getters
- (HDMenuView *)menu
{
    if (!_menu) {
        _menu = [[HDMenuView alloc] initWithFrame:CGRectMake(0, -1, APPFRAME.size.width, 40) titleArray:@[@"今天",@"明天",@"后天"]];
        _menu.delegate = (id)self;
    }
    return _menu;
}

- (HDWaitingTableViewController *)waitingList
{
    if (!_waitingList) {
        _waitingList = [[HDWaitingTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
        _waitingList.tableView.frame = CGRectMake(0, self.menu.bottom, APPFRAME.size.width, self.view.height-self.menu.height);
        _waitingList.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _waitingList.tableView.backgroundColor = [UIColor clearColor];
        _waitingList.delegate = (id)self;
        _waitingList.enableRefresh = YES;
        _waitingList.enableRequest = YES;
    }
    return _waitingList;
}

@end
